<?php
/***************************
 * Mail utility function
 * @author loi-vp
 ***************************/
class myMailUtil
{
	
/**
	 * setup content mail
	 *
	 * @param  array  $arySetting
	 */
	public static function setUpMail($arySetting)
	{
		//$objSMTP = new Swift_Connection_SMTP(sfConfig::get('app_mail_smtp'));
		
		$objSMTP = new Swift_Connection_SMTP(sfConfig::get('app_mail_smtp'));
		$objSMTP->setUsername(sfConfig::get('app_mail_master'));
		$objSMTP->setPassword(sfConfig::get('app_pass_master'));
		$objMail = new myMail($objSMTP);
		$objMail->initialize();

//		$objMail->setSender($arySetting['sender_mail'], $arySetting['sender_name']);
//		$objMail->setFrom($arySetting['sender_mail'], $arySetting['sender_name']);
//		$objMail->setSender(sfConfig::get('app_mail_master'), $arySetting['sender_name']);
//		$objMail->setFrom(sfConfig::get('app_mail_master'), $arySetting['sender_name']);

		$objMail->addTo($aryUser['txtMailAddress']->getValue());
		// Cc
		
		//$objMail->addBcc($arySetting['cc']);
		// Bcc
		//$objMail->addBcc($arySetting['bcc']);

		/* get mail subject */
		$strSubject = myMailUtil::getMailSubject($strType);

		/* get mail template name */
		$strPartialName = myMailUtil::getMailTemplateName($strType);

		/* prepare mail content */
		sfProjectConfiguration::getActive()->loadHelpers('Partial');

		/* get mail content */
		$strContent = get_partial($strPartialName, array('form' => $aryUser));
		
		$pos       = 0;
		$strResult = '';
		while($pos < mb_strlen($strSubject, 'UTF-8'))
		{
			$output = mb_strimwidth($strSubject, $pos, 10, '', 'UTF-8');
			$pos += mb_strlen($output, 'UTF-8');

			$strResult .= mb_encode_mimeheader($output, 'UTF-8', 'UTF-8');
		}
		$objMail->setSubject($strResult);

		$objMail->setBody($strPartialName);

		$objMail->sendMail();

		$objMail->disconnect();
	}
	
	/******************************
	* get mail type create Corporation
	* @return string
	******************************/
	public static function getMailTypeLostPassword()
	{
		return myConstant::MAIL_TYPE_LOST_PASSWORD;
	}
	
/******************************
	* get mail error message when have error
	* @return string error message
	******************************/
	public static function getMailErrorMessage()
	{
		$strMessage  = myConstant::MAIL_ERROR_MESSAGE_1.'<br />';
		$strMessage .= sfConfig::get(myConstant::MAIL_ERROR_NAME);
		$strMessage .= myConstant::MAIL_ERROR_MESSAGE_2;
		return $strMessage;
	}

	/***************************
	* Send email with template support
	* $strType is a setting password kind,
	* @param	array $aryUser
	* 			string $strType
	***************************/
	public static function sendMail($aryUser, $strType)
	{

		/* get mail subject */
		$strSubject = myMailUtil::getMailSubject($strType);

		/* get mail template name */
		$strPartialName = myMailUtil::getMailTemplateName($strType);

		/* prepare mail content */
		sfProjectConfiguration::getActive()->loadHelpers('Partial');

		/* get mail content */
		$strContent = get_partial($strPartialName, array('form' => $aryUser));

		/* if mail template is user and user enter mail address then add to receiver */
	
		$strEmail = $aryUser['txtMailAddress']->getValue();


		/* set mail address to object Swift_RecipientList */
		$objRecipients = new Swift_RecipientList();
		if ($strEmail) {
			$objRecipients->addTo($strEmail);
		}

		try {
			/* if has mail address of receiver then send mail */
			if ($objRecipients->getTo()) {
				$objSMTP = new Swift_Connection_SMTP(sfConfig::get('app_mail_smtp'), 465, Swift_Connection_SMTP::ENC_SSL);
				$objSMTP->setUsername(sfConfig::get('app_mail_master'));
				$objSMTP->setPassword(sfConfig::get('app_pass_master'));
			
				$objMailer = new Swift($objSMTP);

				/* set content for mail */
				$objMessage = new Swift_Message($strSubject, $strContent, 'text/html');

				/* set reply to mail address */
				$objMessage->setReplyTo(myMailUtil::getMailReplyTo());

				/* send mail */
				
				$objMailer->send($objMessage, $objRecipients, myMailUtil::getMailFrom());
				
				$objMailer->disconnect();
				
			}
			
		}
		catch (Exception $ex) {
			/* prepare error message to write log */
			$strErrorMessage = get_partial('mail/mail_error', array('ex' => $ex
																	, 'strMailTo' => $strEmail
																	, 'strSubject' => $strSubject
																	, 'strContent' => $strContent));

				/* write log */
			sfContext::getInstance()->getLogger()->err($strErrorMessage);
			throw $ex;
		}
	}
	
	/******************************
	* get mail subject
	* @param string $strType
	*		 string $strTemplate
	* @return string
	******************************/
	public static function getMailSubject($strType)
	{
		if ($strType == myMailUtil::getMailTypeLostPassword()) {
			return myConstant::SUBJECT_LOST_PASSWORD;
		}
	}
	/******************************
	* get mail template name
	* @param string $strType
	*		 string $strTemplate
	* @return string
	******************************/
	public static function getMailTemplateName($strType)
	{
		if ($strType == myMailUtil::getMailTypeLostPassword()) {
			return myConstant::TEMPLATE_MAIL_LOST_PASSWORD;
		}
	}

	/******************************
	* get smtp server mail
	* @return string smtp mail server name
	******************************/
	public static function getMailSmtp()
	{
		return sfConfig::get(myConstant::MAIL_SMTP_NAME);
	}
	
	/******************************
	* get mail address of sender
	* @return string mail address
	******************************/
	public static function getMailFrom()
	{
		return sfConfig::get(myConstant::MAIL_FROM_NAME);
	}

	/******************************
	* get mail address of reply to
	* @return string mail address
	******************************/
	public static function getMailReplyTo()
	{
		return sfConfig::get(myConstant::MAIL_REPLY_TO_NAME);
	}
	
	/******************************
	* get mail address when have error
	* @return string mail address
	******************************/
	public static function getMailError()
	{
		return sfConfig::get(myConstant::MAIL_ERROR_NAME);
	}
	

	
}
